<?php

class Model_Bug extends Zend_Db_Table_Abstract {

    protected $_name = 'bugs';

    public function createBug($name, $email, $date, $url, $description, $priority, $status) {
// create a new row in the bugs table
        $row = $this->createRow();
// set the row data
        $row->author = $name;
        $row->email = $email;
        $dateObject = new Zend_Date($date);
        $row->date = $dateObject->get(Zend_Date::TIMESTAMP);
        $row->url = $url;
        $row->description = $description;
        $row->priority = $priority;
        $row->status = $status;
// save the new row
// now fetch the id of the row you just created and return it
        $id = $row->save();
        return $id;
    }

    public function fetchBugs() {
        $select = $this->select();
        return $this->fetchAll($select);
    }

    public function updateBug($id, $name, $email, $date, $url, $description, $priority, $status) {
// find the row that matches the id
        $row = $this->find($id)->current();
        if ($row) {
// set the row data
            $row->author = $name;
            $row->email = $email;
            $d = new Zend_Date($date);
            $row->date = $d->get(Zend_Date::TIMESTAMP);
            $row->url = $url;
            $row->description = $description;
            $row->priority = $priority;
            $row->status = $status;
// save the updated row
            $row->save();
            return true;
        } else {
            throw new Zend_Exception("Update function failed; could not find row!");
        }
    }
    
    public function deleteBug($id) {
// find the row that matches the id
        $row = $this->find($id)->current();
        if ($row) {
            $row->delete();
            return true;
        } else {
            throw new Zend_Exception("Delete function failed; could not find row!");
        }
    }

}

?>